package com.activenetwork.smlite.testcase.events.createEvents.basics;

import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.activenetwork.schema.model.event.EventBasicType;
import com.activenetwork.schema.model.event.EventType;
import com.activenetwork.smlite.business.EventBusiness;
import com.activenetwork.smlite.page.generic.ModelPage;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.PageLoader;
import com.activenetwork.util.verification.gson.FailurePoint;

public class Basics022 extends BaseCase {
	static final Logger logger = LoggerFactory.getLogger(Basics022.class);
	private EventBusiness eventBusiness = new EventBusiness();
	private EventType event;
	
	@BeforeClass(alwaysRun = true)
	public void dataSetup() throws DatatypeConfigurationException {
		// create an event
		event = new EventType();
		EventBasicType eventBasic = new EventBasicType();
		
		// event title empty
		eventBasic.setTitle("Test Mandatory Field Event");
		
		// event location
		EventBasicType.Location location = new EventBasicType.Location();
		location.setLocationName("smlite event location 001");
		eventBasic.setLocation(location);
		
		// event length
		EventBasicType.Length.MultiDay multi = new EventBasicType.Length.MultiDay();
		
		
		//Start date
		GregorianCalendar startCalendar = new GregorianCalendar();
		startCalendar.set(Calendar.YEAR, 2013);
		startCalendar.set(Calendar.MONTH, Calendar.FEBRUARY);
		startCalendar.set(Calendar.DATE, 15);
		XMLGregorianCalendar startDateCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(startCalendar);
		multi.setStartDate(startDateCal);
		
		//end date
		GregorianCalendar endCalendar = new GregorianCalendar();
		endCalendar.set(Calendar.YEAR, 2013);
		endCalendar.set(Calendar.MONTH, Calendar.FEBRUARY);
		endCalendar.set(Calendar.DATE, 16);
		XMLGregorianCalendar endDateCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(endCalendar);
		multi.setEndDate(endDateCal);
		
		EventBasicType.Length length = new EventBasicType.Length();
		length.setMultiDay(multi);
		eventBasic.setLength(length);
		
		event.setBasic(eventBasic);
		
	}
	
	@Test
	public void Verify_mandatory_fields_validation_in_Add_Event_page() throws DatatypeConfigurationException{
		loginBusiness.login();
			
		// Verify the event title is mandatory
		event.getBasic().setTitle("");
		eventBusiness.addEventUI(event);
		WebElement error = webDriver.findElement(By.cssSelector(".autotest-text-EventBasicSubview-event-eventname span.help-inline"));
		String msg = error.getText().trim();
		if(!StringUtils.contains(msg, "is required")){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					Basics022.class.getName(), 
					"Verify event title is a mandatory field", 
					"Event title mandatory error message is incorrect: " + msg));
		}
		
		event.getBasic().setTitle("Test Mandatory Field Event");
		msg = null;
		
		
		// Verify the event date is mandatory
		event.getBasic().getLength().setMultiDay(null);
		EventBasicType.Length.OneDay oneDay = new EventBasicType.Length.OneDay();
		oneDay.setEventDate(null);
		
		GregorianCalendar startTime = new GregorianCalendar();
		startTime.set(Calendar.HOUR, 9);
		startTime.set(Calendar.MINUTE, 0);
		oneDay.setStartTime(DatatypeFactory.newInstance().newXMLGregorianCalendar(startTime));
		
		GregorianCalendar endTime = new GregorianCalendar();
		endTime.set(Calendar.HOUR, 17);
		endTime.set(Calendar.MINUTE, 0);
		oneDay.setEndTime(DatatypeFactory.newInstance().newXMLGregorianCalendar(endTime));
		
		event.getBasic().getLength().setOneDay(oneDay);
		
		PageLoader.load(ModelPage.class).events();
		eventBusiness.addEventUI(event);
		error = webDriver.findElement(By.cssSelector(".autotest-date-singledaymeetingdate span.help-inline"));
		msg = error.getText().trim();
		if(!StringUtils.contains(msg, "is required")){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					Basics022.class.getName(), 
					"Verify one-day event date is a mandatory field", 
					"One-day event date mandatory error message is incorrect: " + msg));
		}
		
		GregorianCalendar eventDate = new GregorianCalendar();
		eventDate.set(Calendar.YEAR, 2013);
		eventDate.set(Calendar.MONTH, Calendar.FEBRUARY);
		eventDate.set(Calendar.DATE, 15);
		XMLGregorianCalendar eventDateCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(eventDate);
		
		oneDay.setEventDate(eventDateCal);
		msg = null;
		
		// Verify the event start time is mandatory	
		oneDay.setStartTime(null);
		
		PageLoader.load(ModelPage.class).events();
		eventBusiness.addEventUI(event);
		error = webDriver.findElement(By.cssSelector(".autotest-time-meetingstarttime span.help-inline"));
		msg = error.getText().trim();
		if(!StringUtils.contains(msg, "is required")){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					Basics022.class.getName(), 
					"Verify one-day event start time is a mandatory field", 
					"One-day event start time mandatory error message is incorrect: " + msg));
		}
		
		oneDay.setStartTime(DatatypeFactory.newInstance().newXMLGregorianCalendar(startTime));
		msg = null;
		
		// Verify the event end time is mandatory
		oneDay.setEndTime(null);
		
		PageLoader.load(ModelPage.class).events();
		eventBusiness.addEventUI(event);
		error = webDriver.findElement(By.cssSelector(".autotest-time-meetingendtime span.help-inline"));
		msg = error.getText().trim();
		if(!StringUtils.contains(msg, "is required")){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					Basics022.class.getName(), 
					"Verify one-day event end time is a mandatory field", 
					"One-day event end time mandatory error message is incorrect: " + msg));
		}
		
		oneDay.setEndTime(DatatypeFactory.newInstance().newXMLGregorianCalendar(endTime));
		msg = null;
		
		// Verify the multiple day event start date is mandatory
		event.getBasic().getLength().setOneDay(null);
		EventBasicType.Length.MultiDay multiday = new EventBasicType.Length.MultiDay();
		
		GregorianCalendar startCalendar = new GregorianCalendar();
		startCalendar.set(Calendar.YEAR, 2013);
		startCalendar.set(Calendar.MONTH, Calendar.FEBRUARY);
		startCalendar.set(Calendar.DATE, 15);
		XMLGregorianCalendar startDateCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(startCalendar);
		multiday.setStartDate(startDateCal);
		
		GregorianCalendar endCalendar = new GregorianCalendar();
		endCalendar.set(Calendar.YEAR, 2013);
		endCalendar.set(Calendar.MONTH, Calendar.FEBRUARY);
		endCalendar.set(Calendar.DATE, 16);
		XMLGregorianCalendar endDateCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(endCalendar);
		multiday.setEndDate(endDateCal);
		
		event.getBasic().getLength().setMultiDay(multiday);
		multiday.setStartDate(null);
		
		PageLoader.load(ModelPage.class).events();
		eventBusiness.addEventUI(event);
		error = webDriver.findElement(By.cssSelector(".autotest-date-startdate span.help-inline"));
		msg = error.getText().trim();
		if(!StringUtils.contains(msg, "is required")){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					Basics022.class.getName(), 
					"Verify multiple-day event start date is a mandatory field", 
					"Multiple-day event start date mandatory error message is incorrect: " + msg));
		}
		msg = null;
		multiday.setStartDate(startDateCal);
		
		// Verify the multiple day event end date is mandatory
		multiday.setEndDate(null);
		
		PageLoader.load(ModelPage.class).events();
		eventBusiness.addEventUI(event);
		error = webDriver.findElement(By.cssSelector(".autotest-date-enddate span.help-inline"));
		msg = error.getText().trim();
		if(!StringUtils.contains(msg, "is required")){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					Basics022.class.getName(), 
					"Verify multiple-day event end date is a mandatory field", 
					"Multiple-day event end date mandatory error message is incorrect: " + msg));
		}
		msg = null;
		multiday.setEndDate(endDateCal);
		
		
		// Verify location is mandatory
		event.getBasic().setLocation(null);
		
		PageLoader.load(ModelPage.class).events();
		eventBusiness.addEventUI(event);
		error = webDriver.findElement(By.cssSelector(".autotest-lookup-EventBasicSubview-event-locationref-displaystring span"));
		msg = error.getText().trim();
		if(!StringUtils.contains(msg, "is required")){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					Basics022.class.getName(), 
					"Verify location is a mandatory field", 
					"Location mandatory error message is incorrect: " + msg));
		}
		
		loginBusiness.logout();
	}

}
